package com.project.decoration.controller.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.config.utils.ShiroUtils;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationMessage;
import com.project.decoration.domain.Result;
import com.project.decoration.domain.User;
import com.project.decoration.service.DecorationMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 留言controller
 * @date 2025/02/13 10:42
 */
@Controller
@ResponseBody
@RequestMapping("message")
public class DecorationMessageController {

    @Autowired
    private DecorationMessageService decorationMessageService;

    /** 分页获取留言 */
    @PostMapping("getDecorationMessagePage")
    public Result getDecorationMessagePage(@RequestBody DecorationMessage decorationMessage) {
        Page<DecorationMessage> page = new Page<>(decorationMessage.getPageNumber(),decorationMessage.getPageSize());
        QueryWrapper<DecorationMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationMessage.getName()),DecorationMessage::getName,decorationMessage.getName())
                .like(StringUtils.isNotBlank(decorationMessage.getTel()),DecorationMessage::getTel,decorationMessage.getTel())
                .like(StringUtils.isNotBlank(decorationMessage.getContent()),DecorationMessage::getContent,decorationMessage.getContent())
                .like(StringUtils.isNotBlank(decorationMessage.getCreateBy()),DecorationMessage::getCreateBy,decorationMessage.getCreateBy());
        Page<DecorationMessage> decorationMessagePage = decorationMessageService.page(page, queryWrapper);
        return Result.success(decorationMessagePage);
    }

    /** 根据id获取留言 */
    @GetMapping("getDecorationMessageById")
    public Result getDecorationMessageById(@RequestParam("id")String id) {
        DecorationMessage decorationMessage = decorationMessageService.getById(id);
        return Result.success(decorationMessage);
    }

    @GetMapping("getDecorationMessageByUserId")
    public Result getDecorationMessageByUserId() {
        User userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<DecorationMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationMessage::getUserId,userInfo.getId())
                .orderByDesc(DecorationMessage::getCreateTime);
        List<DecorationMessage> messageList = decorationMessageService.list(queryWrapper);
        return Result.success(messageList);
    }

    /** 保存留言 */
    @PostMapping("saveDecorationMessage")
    public Result saveDecorationMessage(@RequestBody DecorationMessage decorationMessage) {
        User userInfo = ShiroUtils.getUserInfo();
        decorationMessage.setUserId(userInfo.getId());
        boolean save = decorationMessageService.save(decorationMessage);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑留言 */
    @PostMapping("editDecorationMessage")
    public Result editDecorationMessage(@RequestBody DecorationMessage decorationMessage) {
        boolean save = decorationMessageService.updateById(decorationMessage);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除留言 */
    @GetMapping("removeDecorationMessage")
    public Result removeDecorationMessage(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationMessageService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("留言id不能为空！");
        }
    }

}